package com.itheima.template;

import cn.hutool.core.convert.Convert;
import com.baidu.aip.face.AipFace;
import com.itheima.properties.FaceCheckProperties;
import org.json.JSONObject;

import java.util.HashMap;

public class FaceCheckTemplate {
    private FaceCheckProperties properties;

    public FaceCheckTemplate(FaceCheckProperties properties){
        this.properties = properties;
    }

    public int faceCheck(String imageUrl){
        // 初始化一个AipFace
        AipFace client = new AipFace(properties.appId, properties.getApiKey(), properties.getSecretKey());

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 调用接口
        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        return Convert.toInt(res.get("error_code"));
    }
}
